package com.hyr.springcloud.ticket.service;

import com.hyr.springcloud.dto.OrderDTO;
import com.hyr.springcloud.dto.TicketDTO;
import com.hyr.springcloud.service.ITicketService;
import com.hyr.springcloud.ticket.dao.TicketRespository;
import com.hyr.springcloud.ticket.demain.Ticket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class TicketService implements ITicketService {

    private static final Logger LOG = LoggerFactory.getLogger(TicketService.class);

    @Autowired
    private TicketRespository ticketRespository;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Transactional
    @JmsListener(destination = "order:new" ,containerFactory="msgFactory")
    public void handleTicketLock(OrderDTO dto){
        LOG.info("Get new order for ticket lock:{}",dto);
        int lockCount = ticketRespository.lockTicket(dto.getUserinfoId(),dto.getTicketNum());
        if (lockCount == 1){
            dto.setStatus("TICKET_LOCKED");
            jmsTemplate.convertAndSend("order:locked",dto);
        }else{
            dto.setStatus("TICKET_LOCKED_FAIL");
            jmsTemplate.convertAndSend("order:fail",dto);
        }
    }


    @Transactional
    @JmsListener(destination = "order:ticket_move" ,containerFactory="msgFactory")
    public void handleTicketMove(OrderDTO dto){
        LOG.info("Get new order for ticket move:{}",dto);
        int count = ticketRespository.moveTicket(dto.getUserinfoId(),dto.getTicketNum());
        if (count == 0){
            LOG.info("Ticket already moved:{}",dto);
        }

        dto.setStatus("TICKET_MOVE");
        jmsTemplate.convertAndSend("order:finish",dto);

    }


    @Transactional
    @JmsListener(destination = "order:ticket_error" ,containerFactory="msgFactory")
    public void handleTicketUnlock(OrderDTO orderDTO){
        int count = ticketRespository.unLockTicket(orderDTO.getUserinfoId(),orderDTO.getTicketNum());
        if (count == 0){
            LOG.info("Ticket already unlock:{}",orderDTO);
        }
        count = ticketRespository.unMoveTicket(orderDTO.getUserinfoId(),orderDTO.getTicketNum());
        if (count == 0){
            LOG.info("Ticket already unmove,or not moved:{}",orderDTO);
        }
        jmsTemplate.convertAndSend("order:fail",orderDTO);
    }

    @Transactional
    public Ticket ticketLock(OrderDTO dto){
        Ticket ticket = ticketRespository.findByTicketNum(dto.getTicketNum());
        ticket.setLockUser(dto.getUserinfoId());
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ticketRespository.save(ticket);
        return ticket;
    }

    @Override
    @Transactional
    public TicketDTO getTicket(Integer id) {
        Ticket ticket = ticketRespository.getOne(id);
        TicketDTO ticketDTO = new TicketDTO();

        ticketDTO.setId(ticket.getId());
        ticketDTO.setLockUser(ticket.getLockUser());
        ticketDTO.setOwner(ticket.getOwner());
        ticketDTO.setTicketNum(ticket.getTicketNum());
        ticketDTO.setName(ticket.getName());
        jmsTemplate.convertAndSend("order:new",ticket.toString());
        return ticketDTO;
    }

    @Transactional
    public int ticketLock2(OrderDTO orderDTO){
        int lockCount = ticketRespository.lockTicket(orderDTO.getUserinfoId(),orderDTO.getTicketNum());
        LOG.info("update ticket num:{}",lockCount);
        return lockCount;
    }
}
